package labox.innovation.gameserver.model.actor.knownlist;

import java.util.Collection;
import java.util.Map;

import javolution.util.FastList;
import javolution.util.FastMap;
import labox.innovation.gameserver.model.L2Object;
import labox.innovation.gameserver.model.actor.L2Character;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.util.Util;

public class CharKnownList extends ObjectKnownList {
	// =========================================================
	// Data Field
	private Map<Long, FuzePcInstance> _knownPlayers;

	private FastMap<Long, L2Character> _knownCharacters;

	// =========================================================
	// Constructor
	public CharKnownList(L2Character activeChar) {
		super(activeChar);
	}

	// =========================================================
	// Method - Public
	@Override
	public boolean addKnownObject(L2Object object) {
		if (!super.addKnownObject(object))
			return false;
		if (object instanceof L2Character) {
			getKnownCharacters().put(object.getObjectId(), (L2Character) object);
			if (object instanceof FuzePcInstance) {//如果是一个玩家
				getKnownPlayers().put(object.getObjectId(), (FuzePcInstance) object);
			}
		}
		return true;
	}

	@Override
	public boolean removeKnownObject(L2Object object) {
		if (!super.removeKnownObject(object))
			return false;
		if (object instanceof L2Character) {
			getKnownCharacters().remove(object.getObjectId());
			if (object instanceof FuzePcInstance) {
				getKnownPlayers().remove(object.getObjectId());
			}
		}

		//If object is targeted by the L2Character, cancel Attack or Cast
		if (object == getActiveChar().getTarget())
			getActiveChar().setTarget(null);
		return true;
	}

	/**
	 * Return True if the L2PcInstance is in _knownPlayer of the L2Character.<BR><BR>
	 * @param player The L2PcInstance to search in _knownPlayer
	 */
	public final boolean knowsThePlayer(FuzePcInstance player) {//如果是自己或在已知的玩家列表中
		return getActiveChar() == player || getKnownPlayers().containsKey(player.getObjectId());
	}

	public final boolean knowsTheCharacter(L2Character character) {//
		return getActiveChar() == character || getKnownCharacters().containsKey(character.getObjectId());
	}

	/** Remove all L2Object from _knownObjects and _knownPlayer of the L2Character then cancel Attak or Cast and notify AI. */
	@Override
	public final void removeAllKnownRegion() {
		super.removeAllKnownRegion();
		getKnownPlayers().clear();
		getKnownCharacters().clear();

		// Set _target of the L2Character to null
		// Cancel Attack or Cast
		getActiveChar().setTarget(null);
	}

	// =========================================================
	// Method - Private

	// =========================================================
	// Property - Public
	public L2Character getActiveChar() {
		return (L2Character) _activeObject;
	}

	@Override
	public int getDistanceToForgetObject(L2Object object) {
		return 0;
	}

	@Override
	public int getDistanceToWatchObject(L2Object object) {
		return 0;
	}

	public Collection<L2Character> getKnownCharactersInRadius(long radius) {
		FastList<L2Character> result = new FastList<L2Character>();
		Collection<L2Character> plrs = getKnownCharacters().values();
		for (L2Character obj : plrs) {
			if (Util.checkIfInRange((int) radius, getActiveChar(), obj))
				result.add((L2Character) obj);
		}

		return result;
	}

	public final Collection<FuzePcInstance> getKnownPlayersInRadius(long radius) {
		FastList<FuzePcInstance> result = new FastList<FuzePcInstance>();
		Collection<FuzePcInstance> plrs = getKnownPlayers().values();
		for (FuzePcInstance player : plrs)
			if (Util.checkIfInRange((int) radius, getActiveChar(), player))
				result.add(player);
		return result;
	}

	public final Map<Long, FuzePcInstance> getKnownPlayers() {
		if (_knownPlayers == null)
			_knownPlayers = new FastMap<Long, FuzePcInstance>();
		return _knownPlayers;
	}

	public final FastMap<Long, L2Character> getKnownCharacters() {
		if (_knownCharacters == null)
			_knownCharacters = new FastMap<Long, L2Character>();
		return _knownCharacters;
	}

}
